﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Hotfix
{
    /// <summary>
    /// 单例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Singleton<T> where T : class
    {
        private static T _instance = null;

        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    var t = typeof(T);
                    var c = t.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    ConstructorInfo constructorInfo = null;
                    foreach (var item in c)
                    {
                        if (item.GetParameters().Length == 0)
                        {
                            constructorInfo = item;
                            break;
                        }
                    }
                    _instance = (T)constructorInfo?.Invoke(null);
                }
                return _instance;
            }
        }


    }
}
